﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace frm_view
{
    public partial class dbconnect : Form
    {
        public dbconnect()
        {
            InitializeComponent();
        }

        private void btnconnect_Click(object sender, EventArgs e)
        {
            if (cbbAuth.SelectedIndex == 0)
            {
                string strCon = string.Format("Server = {0}; Database = {1}; Integrated security = true;", txtServername.Text, txtDb.Text);
                SqlConnection sqlConn = new SqlConnection(strCon);
                try
                {
                    sqlConn.Open();
                    sqlConn.Close();

                    MessageBox.Show("Ket Noi thanh cong");
                    frm_view.Properties.Settings.Default.dynamic = strCon;
                    frm_view.Properties.Settings.Default.Save();
                }
                catch (Exception)
                {

                    MessageBox.Show("Ket noi that bai");
                }

            }
            else
            {
                string strCon = string.Format("Server={0};Database={1};User Id={2}; Password={3};", txtServername.Text, txtDb.Text, txtUser.Text, txtPass.Text);
                SqlConnection sqlConn = new SqlConnection(strCon);
                try
                {
                    sqlConn.Open();
                    MessageBox.Show("Ket Noi thanh cong");
                    frm_view.Properties.Settings.Default.dynamic = strCon;
                    frm_view.Properties.Settings.Default.Save();
                    sqlConn.Close();
                }
                catch (Exception)
                {

                    MessageBox.Show("Ket noi that bai");
                }
            }
        }

        private void cbbAuth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbAuth.SelectedIndex == 0)
            {
                txtUser.Enabled = false;
                txtPass.Enabled = false;
            }
            else
            {
                txtUser.Enabled = true;
                txtPass.Enabled = true;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
